/**
 * 一些用到的检验规则
 */

export class RuleUtils {
  /**
   * 判断字符串是不是数字
   * @param val
   */
  static isNumber(val: string) {
    if (/^(\-)?\d+(\.\d+)?$/.test(val)) {
      return true
    }
    return '不是正确的数字'
  }

  /**
   * 整数位最多10位，小数位最多4位
   * @param val
   */
  static isNumber10IntAnd4Float(val: string) {
    return RuleUtils.isNumberVal1IntAndVal2Float(val)
  }

  /**
   * 整数位最多val1位，小数位最多val2位
   * @param val
   * @param val1
   * @param val2
   */
  static isNumberVal1IntAndVal2Float(val: string, val1: number = 10, val2: number = 4) {
    if (new RegExp(`^(\\-)?([\\d+]{0,${val1}})(\\.[\\d+]{0,${val2}})?$`).test(val)) {
      return true
    }
    return `整数位最多${val1}位，小数位最多${val2}位`
  }

  /**
   * 手机号码
   * @param val
   */
  static isPhoneNumber(val: string) {
    if (!val) {
      return false
    }
    const data = [
      {
        length: 11,
        prefix: [
          '1340',
          '1341',
          '1342',
          '1343',
          '1344',
          '1345',
          '1346',
          '1347',
          '1348',
          '135',
          '136',
          '137',
          '138',
          '139',
          '147',
          '150',
          '151',
          '152',
          '157',
          '158',
          '159',
          '182',
          '183',
          '184',
          '187',
          '188',
          '178',
          '198',
          '130',
          '131',
          '132',
          '145',
          '155',
          '156',
          '175',
          '176',
          '185',
          '186',
          '166',
          '133',
          '153',
          '173',
          '177',
          '180',
          '181',
          '189',
          '1701',
          '1702',
          '191',
          '199',
          '149',
        ]
      }
    ]
    const find = data.find(i => {
      if (val.length === i.length) {
        const find = i.prefix.find(p => {
          return val.startsWith(p)
        })
        if (find) {
          return true
        }
      }
      return false
    })
    if (find) {
      return true
    }
    return false
  }

  /**
   * 电话号码
   * @param val
   */
  static isTelephoneNumber(val: string) {
    if (/^((\d{3,4}-)|\d{3.4}-)?\d{7,8}$/.test(val)) {
      return true
    }
    if (/\d{3}-\d{8}|\d{4}-\d{7}/.test(val)) {
      return true
    }
    if (/((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)/.test(val)) {
      return true
    }
    return false
  }

  /**
   * 身份证
   */
  static isIdNumber(val: string) {
    if (/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(val)) {
      return true
    }
    return false
  }


  static isEmail(value: string) {
    if (/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(value)) {
      return true
    }
    return false;
  }

  static isChinese(value: string) {
    if (/[\u4e00-\u9fa5]/.test(value)) {
      return true
    }
    return false;
  }
}
